package com.youbug.mall.order_oms.service.impl;

import com.youbug.mall.order_oms.entity.OmsOrderItem;
import com.youbug.mall.order_oms.mapper.OmsOrderItemMapper;
import com.youbug.mall.order_oms.service.IOmsOrderItemService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.youbug.mall.order_oms.entity.OmsOrderItem;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import com.youbug.common.vo.QueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.util.ObjectUtils;

import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 订单项信息 服务实现类
 * </p>
 *
 * @author youbug
 * @since 2024-07-09
 */
@Service
public class OmsOrderItemServiceImpl extends ServiceImpl<OmsOrderItemMapper, OmsOrderItem> implements IOmsOrderItemService {

        @Override
        public IPage<OmsOrderItem> getOmsOrderItemByOrderSns(QueryVo<OmsOrderItem> queryVo) {

        Page<OmsOrderItem> paramPage = new Page<>();

        paramPage.setCurrent(queryVo.getCurrentPage());

        paramPage.setSize(queryVo.getPageSize());

        QueryWrapper<OmsOrderItem> wrapper = new QueryWrapper<>();

        OmsOrderItem omsOrderItem = queryVo.getPayload();

        if(!ObjectUtils.isEmpty(omsOrderItem)){
                    if(omsOrderItem.getId()!=null){
                    wrapper.gt("id", omsOrderItem.getId());
                    }
                    if(omsOrderItem.getOrderId()!=null){
                    wrapper.gt("order_id", omsOrderItem.getOrderId());
                    }
            if(StringUtils.hasText(omsOrderItem.getOrderSn())){
                wrapper.eq("order_sn", omsOrderItem.getOrderSn());
            }
                    if(omsOrderItem.getSpuId()!=null){
                    wrapper.eq("spu_id", omsOrderItem.getSpuId());
                    }
            if(StringUtils.hasText(omsOrderItem.getSpuName())){
                wrapper.eq("spu_name", omsOrderItem.getSpuName());
            }
            if(StringUtils.hasText(omsOrderItem.getSpuPic())){
                wrapper.eq("spu_pic", omsOrderItem.getSpuPic());
            }
            if(StringUtils.hasText(omsOrderItem.getSpuBrand())){
                wrapper.eq("spu_brand", omsOrderItem.getSpuBrand());
            }
                    if(omsOrderItem.getCategoryId()!=null){
                    wrapper.eq("category_id", omsOrderItem.getCategoryId());
                    }
                    if(omsOrderItem.getSkuId()!=null){
                    wrapper.eq("sku_id", omsOrderItem.getSkuId());
                    }
            if(StringUtils.hasText(omsOrderItem.getSkuName())){
                wrapper.eq("sku_name", omsOrderItem.getSkuName());
            }
            if(StringUtils.hasText(omsOrderItem.getSkuPic())){
                wrapper.eq("sku_pic", omsOrderItem.getSkuPic());
            }
                    if(omsOrderItem.getSkuQuantity()!=null){
                    wrapper.gt("sku_quantity", omsOrderItem.getSkuQuantity());
                    }
            if(StringUtils.hasText(omsOrderItem.getSkuAttrsVals())){
                wrapper.eq("sku_attrs_vals", omsOrderItem.getSkuAttrsVals());
            }
                    if(omsOrderItem.getGiftIntegration()!=null){
                    wrapper.gt("gift_integration", omsOrderItem.getGiftIntegration());
                    }
                    if(omsOrderItem.getGiftGrowth()!=null){
                    wrapper.gt("gift_growth", omsOrderItem.getGiftGrowth());
                    }
        }
        if(!ObjectUtils.isEmpty(queryVo.getStartTime())){
            wrapper.gt("create_time", queryVo.getStartTime());
        }
        if(!ObjectUtils.isEmpty(queryVo.getEndTime())){
            wrapper.lt("create_time", queryVo.getEndTime());
        }
        wrapper.orderByDesc("create_time");

        Page<OmsOrderItem> page = this.page(paramPage, wrapper);

        return page;
    }

    @Transactional(transactionManager = "transactionManagerOms", rollbackFor = Exception.class)
        public boolean saveBatch(Collection<OmsOrderItem> entityList) {
        return saveBatch(entityList, DEFAULT_BATCH_SIZE);
    }

    @Override
    public List<OmsOrderItem> getOmsOrderItemByOrderSns(List<String> orderSns) {
        List<OmsOrderItem> omsOrderItems = this.list(new QueryWrapper<OmsOrderItem>().in("order_sn", orderSns));

        return omsOrderItems;
    }
}
